 package server.shop;

 import client.MapleClient;
 import client.inventory.Item;
 import client.inventory.MapleInventoryIdentifier;
 import client.inventory.MapleInventoryType;
 import client.inventory.MaplePet;
 import client.inventory.ModifyInventory;
 import client.skills.SkillFactory;
 import com.fasterxml.jackson.annotation.JsonCreator;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import configs.ServerConfig;
 import constants.GameConstants;
 import constants.ItemConstants;
 import constants.SkillConstants;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import server.AutobanManager;
 import server.MapleInventoryManipulator;
 import server.MapleItemInformationProvider;
 import tools.DateUtil;
 import tools.MaplePacketCreator;
 import tools.Pair;
 import tools.packet.InventoryPacket;
 import tools.packet.NPCPacket;

 public class MapleShop {
   private final int id;
/*  30 */   private final List<Pair<Integer, String>> ranks = new ArrayList<>();

   private final int npcId;
   private final List<MapleShopItem> items;
   private int shopItemId;

   @JsonCreator
   public MapleShop(@JsonProperty("id") int id, @JsonProperty("npcId") int npcId) {
/*  38 */     this.id = id;
/*  39 */     this.npcId = npcId;
/*  40 */     this.shopItemId = 0;
/*  41 */     this.items = new ArrayList<>();
   }

   public void addItem(MapleShopItem item) {
/*  45 */     this.items.add(item);
   }

   public void removeItem(MapleShopItem item) {
/*  49 */     this.items.remove(item);
   }

   public List<MapleShopItem> getItems() {
/*  53 */     return this.items;
   }

   public List<MapleShopItem> getItems(MapleClient c) {
/*  57 */     List<MapleShopItem> itemsPlusRebuy = new ArrayList<>(this.items);
/*  58 */     itemsPlusRebuy.addAll(c.getPlayer().getRebuy());
/*  59 */     return itemsPlusRebuy;
   }

   public void sendShop(MapleClient c) {
/*  63 */     c.getPlayer().setShop(this);
/*  64 */     c.announce(NPCPacket.getNPCShop(getNpcId(), this, c));
   }

   public void sendShop(MapleClient c, int customNpc) {
/*  68 */     c.getPlayer().setShop(this);
/*  69 */     c.announce(NPCPacket.getNPCShop(customNpc, this, c));
   }

   public void sendItemShop(MapleClient c, int itemId) {
/*  73 */     this.shopItemId = itemId;
/*  74 */     c.getPlayer().setShop(this);
/*  75 */     c.announce(NPCPacket.getNPCShop(getNpcId(), this, c));
   }




   public void buy(MapleClient c, int itemId, short quantity, short position) {
/*  82 */     if (c.getPlayer() == null || c.getPlayer().getMap() == null) {
       return;
     }
/*  85 */     if (quantity <= 0) {
/*  86 */       AutobanManager.getInstance().addPoints(c, 1000, 0L, "购买道具数量: " + quantity + " 道具: " + itemId);
       return;
     }
/*  89 */     if (itemId / 10000 == 190 && !GameConstants.isMountItemAvailable(itemId, c.getPlayer().getJob())) {
/*  90 */       c.getPlayer().dropMessage(1, "您无法够买这个道具。");
/*  91 */       c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));


       return;
     }

/*  97 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/*  98 */     MapleShopItem shopItem = findBySlotAndId(c, itemId, position);

/* 100 */     if (shopItem != null && position >= this.items.size()) {
/* 101 */       if (c.getPlayer().getRebuy().isEmpty()) {
/* 102 */         c.announce(MaplePacketCreator.enableActions());
         return;
       }
/* 105 */       int index = position - this.items.size();

/* 107 */       if (shopItem.getRebuy() != null) {

/* 109 */         long price = shopItem.getPrice() * (ItemConstants.isRechargable(itemId) ? 1L : shopItem.getBuyable());
/* 110 */         if (price >= 0L && c.getPlayer().getMeso() >= price) {
/* 111 */           if (MapleInventoryManipulator.checkSpace(c, itemId, quantity, "")) {
/* 112 */             c.getPlayer().gainMeso(-price, false);
/* 113 */             MapleInventoryManipulator.addbyItem(c, shopItem.getRebuy());
/* 114 */             c.getPlayer().getRebuy().remove(index);
/* 115 */             c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, index, itemId));
           } else {
/* 117 */             c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.背包空间不够, this, c, -1, itemId));
           }
         } else {
/* 120 */           c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));
         }
       } else {
/* 123 */         c.getPlayer().dropMessage(1, "购买回购栏道具出现错误.");
/* 124 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));
       }
/* 126 */     } else if (shopItem != null && shopItem.getPrice() > 0 && shopItem.getReqItem() == 0) {
/* 127 */       if (c.getPlayer().getLevel() < shopItem.getMinLevel()) {
/* 128 */         c.getPlayer().dropMessage(1, "Lv:" + shopItem.getMinLevel() + "以上才可购买。");
/* 129 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));
         return;
       }
/* 132 */       if (shopItem.getCategory() >= 0) {
/* 133 */         boolean passed = true;
/* 134 */         int y = 0;
/* 135 */         for (Pair<Integer, String> i : getRanks()) {
/* 136 */           if (c.getPlayer().haveItem(((Integer)i.left).intValue(), 1, true, true) && shopItem.getCategory() >= y) {
/* 137 */             passed = true;
             break;
           }
/* 140 */           y++;
         }
/* 142 */         if (!passed) {
/* 143 */           c.getPlayer().dropMessage(1, "You need a higher rank.");
/* 144 */           c.announce(MaplePacketCreator.enableActions());
           return;
         }
       }
/* 148 */       long price = ItemConstants.isRechargable(itemId) ? shopItem.getPrice() : (shopItem.getPrice() * quantity);
/* 149 */       long meso = (shopItem.getPointtype() == 0) ? c.getPlayer().getMeso() : c.getPlayer().getPQPoint();
/* 150 */       if (price >= 0L && meso >= price) {
/* 151 */         if (MapleInventoryManipulator.checkSpace(c, itemId, quantity, "")) {
/* 152 */           if (shopItem.getPointtype() == 0) {
/* 153 */             c.getPlayer().gainMeso(-price, false);
           } else {
/* 155 */             c.getPlayer().gainPQPoint(-price);
           }
/* 157 */           if (ItemConstants.isPet(itemId)) {
/* 158 */             MapleInventoryManipulator.addById(c, itemId, quantity, "", MaplePet.createPet(itemId, MapleInventoryIdentifier.getInstance()), -1L, "Bought from shop " + this.id + ", " + this.npcId + " on " + DateUtil.getCurrentDate());
           }
/* 160 */           else if (!ItemConstants.isRechargable(itemId)) {
/* 161 */             int state = shopItem.getState();
/* 162 */             long period = shopItem.getPeriod();
/* 163 */             MapleInventoryManipulator.addById(c, itemId, quantity, period, state, "商店购买 " + this.id + ", " + this.npcId + " 时间 " + DateUtil.getCurrentDate());
           } else {
/* 165 */             quantity = ii.getSlotMax(shopItem.getItemId());
/* 166 */             MapleInventoryManipulator.addById(c, itemId, quantity, "商店购买 " + this.id + ", " + this.npcId + " 时间 " + DateUtil.getCurrentDate());
           }

/* 169 */           c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));
         } else {
/* 171 */           c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.背包空间不够, this, c, -1, itemId));
         }
       }
/* 174 */     } else if (shopItem != null && shopItem.getReqItem() > 0 && shopItem.getReqItemQ() > 0 && c.getPlayer().haveItem(shopItem.getReqItem(), shopItem.getReqItemQ() * quantity, false, true)) {
/* 175 */       if (MapleInventoryManipulator.checkSpace(c, itemId, quantity, "")) {
/* 176 */         MapleInventoryManipulator.removeById(c, ItemConstants.getInventoryType(shopItem.getReqItem()), shopItem.getReqItem(), shopItem.getReqItemQ() * quantity, false, false);
/* 177 */         if (ItemConstants.isPet(itemId)) {
/* 178 */           MapleInventoryManipulator.addById(c, itemId, quantity, "", MaplePet.createPet(itemId, MapleInventoryIdentifier.getInstance()), -1L, "商店购买 " + this.id + ", " + this.npcId + " 时间 " + DateUtil.getCurrentDate());
         }
/* 180 */         else if (!ItemConstants.isRechargable(itemId)) {
/* 181 */           int state = shopItem.getState();
/* 182 */           long period = shopItem.getPeriod();
/* 183 */           MapleInventoryManipulator.addById(c, itemId, quantity, period, state, "商店购买 " + this.id + ", " + this.npcId + " 时间 " + DateUtil.getCurrentDate());
         } else {
/* 185 */           quantity = ii.getSlotMax(shopItem.getItemId());
/* 186 */           MapleInventoryManipulator.addById(c, itemId, quantity, "商店购买 " + this.id + ", " + this.npcId + " 时间 " + DateUtil.getCurrentDate());
         }

/* 189 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.购买道具完成, this, c, -1, itemId));
       } else {
/* 191 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.背包空间不够, this, c, -1, itemId));
       }
     }
   }




   public void sell(MapleClient c, MapleInventoryType type, short slot, short quantity) {
/* 200 */     if (quantity == 65535 || quantity == 0) {
/* 201 */       quantity = 1;
     }
/* 203 */     Item item = c.getPlayer().getInventory(type).getItem(slot);
/* 204 */     if (item == null) {
       return;
     }
/* 207 */     if (ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
/* 208 */       quantity = item.getQuantity();
     }
/* 210 */     if (item.getItemId() == 4000463) {
/* 211 */       c.getPlayer().dropMessage(1, "该道具无法卖出.");
/* 212 */       c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.卖出道具完成, this, c, -1, item.getItemId()));
       return;
     }
/* 215 */     if (quantity < 0) {
/* 216 */       AutobanManager.getInstance().addPoints(c, 1000, 0L, "卖出道具 " + quantity + " " + item.getItemId() + " (" + type.name() + "/" + slot + ")");
       return;
     }
/* 219 */     short iQuant = item.getQuantity();
/* 220 */     if (iQuant == 65535) {
/* 221 */       iQuant = 1;
     }
/* 223 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 224 */     if (ii.cantSell(item.getItemId()) || ItemConstants.isPet(item.getItemId())) {
       return;
     }
/* 227 */     if (quantity <= iQuant && iQuant > 0) {
       double price;
/* 229 */       if (ItemConstants.is飞镖道具(item.getItemId()) || ItemConstants.is子弹道具(item.getItemId())) {
/* 230 */         price = ii.getUnitPrice(item.getItemId()) / ii.getSlotMax(item.getItemId());
       } else {
/* 232 */         price = ii.getPrice(item.getItemId());
       }
/* 234 */       long recvMesos = (long)Math.ceil(price * quantity);
/* 235 */       if (c.getPlayer().getMeso() + recvMesos > ServerConfig.CHANNEL_PLAYER_MAXMESO) {
/* 236 */         c.getPlayer().dropMessage(1, "携带金币不能超过" + ServerConfig.CHANNEL_PLAYER_MAXMESO + ".");
/* 237 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.卖出道具完成, this, c, -1, item.getItemId()));

         return;
       }
/* 241 */       if (item.getQuantity() == quantity) {
/* 242 */         c.getPlayer().getRebuy().add(new MapleShopItem(item.copy(), ii.getPrice(item.getItemId()), item.getQuantity()));
       } else {
/* 244 */         c.getPlayer().getRebuy().add(new MapleShopItem(item.copyWithQuantity(quantity), ii.getPrice(item.getItemId()), quantity));
       }
/* 246 */       MapleInventoryManipulator.removeFromSlot(c, type, slot, quantity, false);
/* 247 */       if (price != -1.0D && recvMesos > 0L) {
/* 248 */         c.getPlayer().gainMeso(recvMesos, false);
       }
/* 250 */       c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.卖出道具完成, this, c, -1, item.getItemId()));
     }
   }




   public void recharge(MapleClient c, short slot) {
/* 258 */     Item item = c.getPlayer().getInventory(MapleInventoryType.USE).getItem(slot);
/* 259 */     if (item == null || (!ItemConstants.is飞镖道具(item.getItemId()) && !ItemConstants.is子弹道具(item.getItemId()))) {
       return;
     }
/* 262 */     MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
/* 263 */     short slotMax = ii.getSlotMax(item.getItemId());
/* 264 */     int skill = SkillConstants.getMasterySkill(c.getPlayer().getJob());
/* 265 */     if (skill != 0) {
/* 266 */       slotMax = (short)(slotMax + c.getPlayer().getTotalSkillLevel(SkillFactory.getSkill(skill)) * 10);
     }
/* 268 */     if (item.getQuantity() < slotMax) {
/* 269 */       int price = (int)Math.round(ii.getUnitPrice(item.getItemId()) * (slotMax - item.getQuantity()));
/* 270 */       if (c.getPlayer().getMeso() >= price) {
/* 271 */         item.setQuantity(slotMax);
/* 272 */         c.announce(InventoryPacket.modifyInventory(false, Collections.singletonList(new ModifyInventory(1, item))));
/* 273 */         c.getPlayer().gainMeso(-price, false, false);
/* 274 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.充值飞镖完成, this, c, -1, item.getItemId()));
       } else {
/* 276 */         c.announce(NPCPacket.confirmShopTransaction(MapleShopResponse.充值金币不够, this, c, -1, item.getItemId()));
       }
     }
   }

   protected MapleShopItem findBySlotAndId(int itemId, int slot) {
/* 282 */     MapleShopItem shopItem = this.items.get(slot);
/* 283 */     if (shopItem != null && shopItem.getItemId() == itemId) {
/* 284 */       return shopItem;
     }
/* 286 */     return null;
   }

   protected MapleShopItem findBySlotAndId(MapleClient c, int itemId, int pos) {
/* 290 */     MapleShopItem shopItem = getItems(c).get(pos);

/* 292 */     if (shopItem != null && shopItem.getItemId() == itemId) {
/* 293 */       return shopItem;
     }
/* 295 */     return null;
   }
































































   public int getNpcId() {
/* 362 */     return this.npcId;
   }

   public int getId() {
/* 366 */     return this.id;
   }

   public int getShopItemId() {
/* 370 */     return this.shopItemId;
   }

   public List<Pair<Integer, String>> getRanks() {
/* 374 */     return this.ranks;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\shop\MapleShop.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */